package com.longbig.multifunction.helper;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringHelper {

    public static String getIp(String data) {
        String ipAddressRegex = "\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b";
        Pattern pattern = Pattern.compile(ipAddressRegex);
        Matcher matcher = pattern.matcher(data);

        List<String> matchIps = Lists.newArrayList();
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }


    /**
     * 提取出字符串中所有的IP
     * @param data
     * @return
     */
    public static Set<String> getAllIp(String data) {
        Set<String> matchIps = Sets.newHashSet();
        if (StringUtils.isEmpty(data)) {
            return matchIps;
        }
        String ipAddressRegex = "\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b";
        Pattern pattern = Pattern.compile(ipAddressRegex);
        Matcher matcher = pattern.matcher(data);

        while (matcher.find()) {
            matchIps.add(matcher.group());
        }
        return matchIps;
    }



    public static void main(String[] args) throws Exception {
        String s = "root     pts/2        10.254.213.47    Thu Oct 12 16:44 - 17:13  (00:29)    \n" +
                "root     pts/2        10.254.213.47    Thu Oct 12 16:35 - 16:44  (00:08)    \n" +
                "root     pts/1        10.254.213.47    Thu Oct 12 16:35 - 18:52  (02:17)    \n" +
                "root     pts/1        10.254.213.47    Thu Oct 12 16:29 - 16:31  (00:02)    \n" +
                "root     pts/3        10.254.213.47    Thu Oct 12 16:09 - 16:35  (00:25)    \n" +
                "root     pts/1        10.254.213.47    Thu Oct 12 15:38 - 16:24  (00:45)    \n" +
                "root     pts/0        10.254.213.47    Thu Oct 12 15:05 - 18:59  (03:54)    \n" +
                "root     pts/1        10.254.213.47    Thu Oct 12 14:57 - 14:57  (00:00)    \n" +
                "root     pts/2        10.254.213.47    Thu Oct 12 14:45 - 16:23  (01:38)    \n" +
                "root     pts/1        10.254.213.47    Thu Oct 12 14:06 - 14:54  (00:48)    \n" +
                "fabric_t pts/2        10.254.222.105   Thu Oct 12 10:13 - 10:13  (00:00)    \n" +
                "root     pts/1        10.254.213.47    Thu Oct 12 10:04 - 12:24  (02:20)    \n" +
                "fabric_t pts/0        10.254.213.47    Thu Oct 12 10:03 - 15:04  (05:01)    \n" +
                "root     pts/0        10.254.213.47    Thu Oct 12 09:59 - 10:00  (00:00)    \n" +
                "fabric_t pts/1        10.254.213.47    Thu Oct 12 09:53 - 09:56  (00:03)    \n" +
                "fabric_t pts/0        10.254.213.47    Thu Oct 12 09:49 - 09:56  (00:06)    \n" +
                "root     pts/0        10.254.213.47    Wed Oct 11 18:41 - 21:12  (02:30)    \n" +
                "root     pts/0        10.254.213.47    Wed Oct 11 18:40 - 18:40  (00:00)    \n" +
                "root     pts/0        10.254.213.47    Wed Oct 11 18:03 - 18:37  (00:34)    \n" +
                "root     pts/0        10.254.213.47    Wed Oct 11 14:42 - 17:10  (02:27)    \n" +
                "fabric_t pts/2        10.254.222.105   Tue Oct 10 17:41 - 17:41  (00:00)    \n" +
                "fabric_t pts/2        10.254.222.105   Tue Oct 10 17:40 - 17:40  (00:00)    \n" +
                "root     pts/1        10.254.213.47    Tue Oct 10 17:29 - 20:22  (02:53)    \n" +
                "fabric_t pts/1        10.254.222.105   Tue Oct 10 17:07 - 17:07  (00:00)    \n" +
                "fabric_t pts/1        10.254.222.105   Tue Oct 10 17:06 - 17:06  (00:00)    \n" +
                "root     pts/0        10.254.213.47    Tue Oct 10 17:06 - 19:19  (02:12)    \n" +
                "root     pts/0        10.254.213.47    Tue Oct 10 17:06 - 17:06  (00:00)    \n" +
                "root     pts/0        10.254.213.47    Sat Oct  7 09:52 - 12:04  (02:11)    \n" +
                "root     pts/0        10.254.213.47    Wed Sep 27 10:54 - 13:26  (02:32)    \n" +
                "root     pts/0        10.254.213.47    Fri Sep 22 15:43 - 18:45  (03:01)    \n" +
                "root     pts/0        10.254.213.47    Thu Sep 21 15:59 - 18:50  (02:50)    \n" +
                "reboot   system boot  3.10.0-957.el7.x Sun Sep 17 18:13 - 17:09 (93+22:55)  \n" +
                "reboot   system boot  3.10.0-957.el7.x Sun Sep 17 17:05 - 17:09 (94+00:03)  \n" +
                "user     pts/1        10.254.222.102   Mon Sep 11 18:24 - 20:35  (02:11)    \n" +
                "root     pts/1        10.254.213.47    Mon Sep 11 17:50 - 17:50  (00:00)    \n" +
                "root     pts/0        10.254.213.47    Mon Sep 11 15:37 - 20:46  (05:08)    \n" +
                "reboot   system boot  3.10.0-957.el7.x Mon Sep 11 10:57 - 17:09 (100+06:11) \n" +
                "root     pts/1        10.254.213.47    Tue Aug  8 09:43 - 11:58  (02:14)    \n" +
                "root     pts/0        10.254.213.47    Tue Aug  8 09:24 - 11:42  (02:18)    \n";
        System.out.println(getAllIp(s));


    }
}
